package com.augurit.common.system.convert;

import java.util.ArrayList;
import java.util.List;

import com.augurit.common.base.extmodel.base.ITreeNode;
import com.augurit.common.base.extmodel.base.LinkCheckTreeNode;
import com.augurit.common.base.extmodel.inner.AppTreeNode;
import com.augurit.common.constant.OrgConstant;
import com.augurit.common.entity.AcFunction;
import com.augurit.common.entity.AcUserFunc;
import com.augurit.common.syscode.web.form.SysCodeForm;
import com.augurit.common.system.util.OrgUtils;
import com.augurit.common.system.web.org.form.AcFunctionForm;
import com.augurit.common.system.web.org.form.AcUserFuncForm;

public class AcFunctionConverter {
	
	public static void convertToEntity(AcFunctionForm form, AcFunction entity){
		if(form != null && entity != null){
			
			if(form.getFuncId() != null)
				entity.setFuncId(form.getFuncId());
			
			if(form.getFuncCode() != null && form.getFuncCode().trim().length() > 0)
				entity.setFuncCode(form.getFuncCode());
			
			if(form.getFuncName() != null && form.getFuncName().trim().length() > 0)
				entity.setFuncName(form.getFuncName());
			
			if(form.getModuleId() != null)
				entity.setModuleId(form.getModuleId());
			
			if(form.getFuncType() != null && form.getFuncType().trim().length() > 0)
				entity.setFuncType(form.getFuncType());
			
			if(form.getFuncUrl() != null && form.getFuncUrl().trim().length() > 0)
				entity.setFuncUrl(form.getFuncUrl());
			
			if(form.getUrlType() != null && form.getUrlType().trim().length() > 0)
				entity.setUrlType(form.getUrlType());
			
			if(form.getIsCheck() != null && form.getIsCheck().trim().length() > 0)
				entity.setIsCheck(form.getIsCheck());
			
			if(form.getIsMenu() != null && form.getIsMenu().trim().length() > 0)
				entity.setIsMenu(form.getIsMenu());
			
			if(form.getFuncDesc() != null && form.getFuncDesc().trim().length() > 0)
				entity.setFuncDesc(form.getFuncDesc());
			
			if(form.getRemark() != null && form.getRemark().trim().length() > 0)
				entity.setRemark(form.getRemark());
		}
	}
	
	public static void convertToEntity(AcUserFuncForm form, AcUserFunc entity){
		if(form != null && entity != null){

			if(form.getUfId() != null)
				entity.setUfId(form.getUfId());
			
			if(form.getUserId() != null)
				entity.setUserId(form.getUserId());
			
			if(form.getFuncId() != null)
				entity.setFuncId(form.getFuncId());
			
			if(form.getAuthType() != null && form.getAuthType().trim().length() > 0)
				entity.setAuthType(form.getAuthType());
			
			if(form.getAppId() != null)
				entity.setAppId(form.getAppId());
			
			if(form.getModuleId() != null)
				entity.setModuleId(form.getModuleId());
			
			if(form.getStartDate() != null)
				entity.setStartDate(form.getStartDate());
			
			if(form.getEndDate() != null)
				entity.setEndDate(form.getEndDate());
			
			if(form.getRemark() != null && form.getRemark().trim().length() > 0)
				entity.setRemark(form.getRemark());
		}
	}
	
	public static AcFunctionForm convertToForm(AcFunction entity){
		if(entity != null){
			AcFunctionForm form = new AcFunctionForm();
			
			form.setFuncId(entity.getFuncId());
			form.setFuncCode(entity.getFuncCode());
			form.setFuncName(entity.getFuncName());
			form.setModuleId(entity.getModuleId());
			form.setFuncType(entity.getFuncType());
			form.setFuncUrl(entity.getFuncUrl());
			form.setUrlType(entity.getUrlType());
			form.setIsCheck(entity.getIsCheck());
			form.setIsMenu(entity.getIsMenu());
			form.setFuncDesc(entity.getFuncDesc());
			form.setRemark(entity.getRemark());
			
			return form;
		}else
			return null;
	}
	
	public static AcUserFuncForm convertToForm(AcUserFunc entity){
		if(entity != null){
			AcUserFuncForm form = new AcUserFuncForm();

			form.setUfId(entity.getUfId());
			form.setUserId(entity.getUserId());
			form.setFuncId(entity.getFuncId());
			form.setAuthType(entity.getAuthType());
			form.setAppId(entity.getAppId());
			form.setModuleId(entity.getModuleId());
			form.setStartDate(entity.getStartDate());
			form.setEndDate(entity.getEndDate());
			form.setRemark(entity.getRemark());
			
			return form;
		}else
			return null;
	}
	
	public static List<AcFunctionForm> convertToFormList(List<AcFunction> list){
		return convertToFormList(list, null);
	}
	
	public static List<AcFunctionForm> convertToFormList(List<AcFunction> list, List<SysCodeForm> funcTypeList){
		if(list != null && list.size() > 0){
			List<AcFunctionForm> result = new ArrayList();
			
			for(AcFunction entity : list){
				AcFunctionForm form = convertToForm(entity);
				form.setFuncTypeLabel(OrgUtils.getItemName(form.getFuncType(), funcTypeList));
				result.add(form);
			}
			
			return result;
		}else
			return null;
	}
	
	/**
	 * 将功能form对象转换为ExtCheckTreeUrlNode对象
	 * @param form
	 * @return
	 */
	public static LinkCheckTreeNode convertFuncToCheckTreeUrlNode(AcFunctionForm form, Long appId, String appUrl){
		LinkCheckTreeNode node = null;
		if( form != null){
			node = new LinkCheckTreeNode();
			node.setId(OrgUtils.getFuncNo(AcFunction.class.getSimpleName(), appId, form.getFuncId()));
			node.setText(form.getFuncName());
			node.setIconCls(ITreeNode.DEFAULT_LEAF_TREE_NODE_CLS);
			node.setLeaf(true);
			node.setChecked(false);
			
			//计算功能调用入口
			if(form.getUrlType().equals(OrgConstant.ZERO)){
				//相对路径
				node.setUrl(appUrl + form.getFuncUrl());
			}else{
				//绝对路径 或 javascript脚本
				node.setUrl(form.getFuncUrl());
			}		
		}
		return node;
	}
	
	/**
	 * 将功能form对象转换为AppTreeNode对象
	 * @param form
	 * @return
	 */
	public static AppTreeNode convertFuncToTreeNode(AcFunctionForm form){
		AppTreeNode node = null;
		if( form != null){
			node = new AppTreeNode();
			node.setId(AppTreeNode.getKey(AcFunction.class.getSimpleName(), form.getFuncId()));
			node.setText(form.getFuncName());
			node.setQtip(form.getFuncCode());
			node.setIconCls(ITreeNode.DEFAULT_LEAF_TREE_NODE_CLS);
			node.setLeaf(true);
		}
		return node;
	}
	
	/**
	 * 将功能form对象转换为ExtCheckTreeNode对象
	 * @param form
	 * @return
	 */
	public static LinkCheckTreeNode convertFuncToCheckTreeNode(AcFunctionForm form){
		LinkCheckTreeNode node = null;
		if( form != null){
			node = new LinkCheckTreeNode();
			node.setId(AppTreeNode.getKey(AcFunction.class.getSimpleName(), form.getFuncId()));
			node.setText(form.getFuncName());
			node.setIconCls(ITreeNode.DEFAULT_LEAF_TREE_NODE_CLS);
			node.setLeaf(true);
			node.setChecked(false);
		
		}
		return node;
	}
}
